<template>
  <div class="register-container">
    <div class="register-box">
      <div class="left-section">
        <div class="welcome-text">
          <h2>欢迎加入</h2>
          <p>高校勤工助学管理系统</p>
        </div>
        <div class="decoration">
          <img src="@/assets/register-decoration.svg" alt="decoration">
        </div>
      </div>
      
      <div class="right-section">
        <el-form
          ref="registerFormRef"
          :model="registerForm"
          :rules="registerRules"
          class="register-form"
        >
          <h3 class="title">用户注册</h3>
          
          <el-form-item prop="role">
            <el-radio-group v-model="registerForm.role" class="role-group">
              <el-radio-button label="STUDENT">学生</el-radio-button>
              <el-radio-button label="TEACHER">教师</el-radio-button>
            </el-radio-group>
          </el-form-item>

          <el-form-item prop="username">
            <el-input
              v-model="registerForm.username"
              placeholder="用户名"
            >
              <template #prefix>
                <el-icon><i-ep-user /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              v-model="registerForm.password"
              :type="passwordVisible ? 'text' : 'password'"
              placeholder="密码"
            >
              <template #prefix>
                <el-icon><i-ep-lock /></el-icon>
              </template>
              <template #suffix>
                <el-icon 
                  class="cursor-pointer"
                  @click="passwordVisible = !passwordVisible"
                >
                  <i-ep-view v-if="passwordVisible" />
                  <i-ep-hide v-else />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item prop="confirmPassword">
            <el-input
              v-model="registerForm.confirmPassword"
              :type="passwordVisible ? 'text' : 'password'"
              placeholder="确认密码"
            />
          </el-form-item>

          <el-form-item prop="name">
            <el-input
              v-model="registerForm.name"
              placeholder="姓名"
            />
          </el-form-item>

          <el-form-item prop="email">
            <el-input
              v-model="registerForm.email"
              placeholder="邮箱"
            />
          </el-form-item>

          <el-form-item prop="phone">
            <el-input
              v-model="registerForm.phone"
              placeholder="手机号"
            />
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              :loading="loading"
              class="submit-btn"
              @click="handleRegister"
            >
              注册
            </el-button>
          </el-form-item>

          <div class="login-link">
            已有账号？
            <router-link to="/login">立即登录</router-link>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'

export default {
  name: 'RegisterPage',
  setup() {
    const router = useRouter()
    const store = useStore()
    const registerFormRef = ref(null)
    const loading = ref(false)
    const passwordVisible = ref(false)

    const registerForm = reactive({
      role: 'STUDENT',
      username: '',
      password: '',
      confirmPassword: '',
      name: '',
      email: '',
      phone: ''
    })

    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else if (value.length < 6) {
        callback(new Error('密码长度不能小于6位'))
      } else {
        if (registerForm.confirmPassword !== '') {
          registerFormRef.value.validateField('confirmPassword')
        }
        callback()
      }
    }

    const validateConfirmPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== registerForm.password) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }

    const registerRules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
      ],
      password: [{ validator: validatePass, trigger: 'blur' }],
      confirmPassword: [{ validator: validateConfirmPass, trigger: 'blur' }],
      name: [
        { required: true, message: '请输入姓名', trigger: 'blur' },
        { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
      ],
      email: [
        { required: true, message: '请输入邮箱地址', trigger: 'blur' },
        { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
      ],
      phone: [
        { required: true, message: '请输入手机号', trigger: 'blur' },
        { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
      ]
    }

    const handleRegister = async () => {
      if (!registerFormRef.value) return

      try {
        await registerFormRef.value.validate()
        loading.value = true

        // 调用注册 API
        await store.dispatch('register', registerForm)
        ElMessage.success('注册成功')
        router.push('/login')
      } catch (error) {
        console.error('Register error:', error)
      } finally {
        loading.value = false
      }
    }

    return {
      registerFormRef,
      registerForm,
      registerRules,
      loading,
      passwordVisible,
      handleRegister
    }
  }
}
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.register-box {
  width: 1000px;
  height: 600px;
  background: #fff;
  border-radius: 15px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  display: flex;
  overflow: hidden;
}

.left-section {
  flex: 1;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  color: white;
}

.welcome-text {
  text-align: center;
}

.welcome-text h2 {
  font-size: 32px;
  margin-bottom: 10px;
}

.welcome-text p {
  font-size: 18px;
  opacity: 0.9;
}

.decoration {
  text-align: center;
}

.decoration img {
  width: 80%;
  max-width: 300px;
}

.right-section {
  flex: 1;
  padding: 40px;
  overflow-y: auto;
}

.register-form {
  max-width: 380px;
  margin: 0 auto;
}

.title {
  font-size: 24px;
  color: #333;
  text-align: center;
  margin-bottom: 30px;
}

.role-group {
  width: 100%;
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}

.submit-btn {
  width: 100%;
  padding: 12px 0;
}

.login-link {
  text-align: center;
  margin-top: 15px;
  color: #666;
}

.login-link a {
  color: #667eea;
  text-decoration: none;
}

.login-link a:hover {
  text-decoration: underline;
}

:deep(.el-input__wrapper) {
  padding: 0 11px;
}

:deep(.el-input__inner) {
  height: 40px;
}

:deep(.el-form-item__content) {
  line-height: 40px;
}
</style> 